<?php
// Explode path for base()->urlArray
if (isset($_SERVER['PATH_INFO'])) {
	base()->urlArray = explode('/', 
		str_replace('\\', '/', $_SERVER['PATH_INFO']));
}

// Define root location
if (isset(base()->config['root'])) base()->urlArray[0] = base()->config['root'];
else {
	$path = explode('/index.php', $_SERVER['SCRIPT_NAME']);
	base()->urlArray[0] = $path[0];
}

#### URL #######################################################################
function url($param1 = false, $param2 = false) {
	
	// If the supplied element exists in the url array, return it
	// e.g. url(1)
	if (is_numeric($param1) && $param2 == false) {
		if (isset(base()->urlArray[$param1])) return base()->urlArray[$param1];
		else return false;
	}

	// If we are returning a few elements of the array elements not just one
	// e.g. url(2, 5), url(2, true);
	else if (is_numeric($param1) && $param2 != false) {
        if ($param2 === true) {
			$url_array = base()->urlArray;
			$url_array = array_slice($url_array, 
				count($url_array) - 1);
        }
		else $temp_url_array = array_slice(base()->urlArray, $param1, $param2);

		if ($url_array) return implode('/', $url_array); 
		else return false;
	}

	// If element is a string return a bool for a searched value
	// e.g. url('search', 'confirm')	
	else if ($param1 == 'search') return (strstr(implode('/', base()->urlArray), $param2));

	// If url() is called with no parameters return the domain with root
	else return base()->urlArray[0];
}

#### HEADERS ###################################################################
function header201() { header('HTTP/1.1 201 Created'); }
function header301() { header('HTTP/1.1 301 Moved Permanently'); }
function header404() { header('HTTP/1.1 404 Not Found'); }
function location($url) { 
	if(!strstr($url, 'http')) $url = url().'/'.$url;
	header ('Location: '.$url); die(); 
}

#### SLUG ######################################################################
function slug($string) {
	$replace_with_dash[] = ' ';
	
	$replace_custom[] = '&';
	$replace_with_custom[] = 'and';
	$replace_custom[] = '---';
	$replace_with_custom[] = '-';
	$replace_custom[] = '--';
	$replace_with_custom[] = '-';	

	$replace_with_null[] = '\'';
	$replace_with_null[] = '"';
	$replace_with_null[] = '/';
	$replace_with_null[] = '\\';
	$replace_with_null[] = '|';
	$replace_with_null[] = '?';
	$replace_with_null[] = ',';
	$replace_with_null[] = '#';
	$replace_with_null[] = ':';
	$replace_with_null[] = ';';
	$replace_with_null[] = '(';
	$replace_with_null[] = ')';
	$replace_with_null[] = '^';
	$replace_with_null[] = '%';
	$replace_with_null[] = '$';
	$replace_with_null[] = '@';
	$replace_with_null[] = '!';
	$replace_with_null[] = '[';
	$replace_with_null[] = ']';
	$replace_with_null[] = '{';
	$replace_with_null[] = '}';
	$replace_with_null[] = '~';
	$replace_with_null[] = '`';
	$replace_with_null[] = '_';
	$replace_with_null[] = '+';
	$replace_with_null[] = '=';
	$replace_with_null[] = '.';
	$replace_with_null[] = '.';
	$replace_with_null[] = '“';
	$replace_with_null[] = '”';
	
	$string = str_replace($replace_with_dash, '-', $string);
	$string = str_replace($replace_custom, $replace_with_custom, $string);
	$string = str_replace($replace_with_null, null, $string);
    return strtolower(trim($string));
}

#### URL ARRAY #################################################################
function urlArray($clean = false) { 
	if (!$clean) return base()->urlArray; 
	$clean = array();
	foreach (urlArray() as $key => $value) $clean[] = cleanMethodName($value);
	return $clean;
}

#### AUTOLINK ##################################################################
// http://webdesign.anmari.com/getting-the-urls-and-only-the-urls-with-ereg - thanks!
function autolink($text, $url_text = false) {
	if (!$url_text) $url_text = '\\0';
	$text = preg_replace('[a-zA-Z]+://([.]?[a-zA-Z0-9-])*([/]?[a-zA-Z0-9_-])*([/a-zA-Z0-9?&#\._=-]*)',
	 	"<a href=\"\\0\" title=\"\\0\" rel=\"nofollow\">$url_text</a>", $text);
	return $text;
}
?>